package com.task.core.service.impl;

import com.task.common.annotation.DataScope;
import com.task.common.constant.ErrorCodeConstants;
import com.task.common.domain.info.KeyAndValue;
import com.task.common.exception.ServiceException;
import com.task.common.utils.SecurityUtils;
import com.task.common.utils.StringUtils;
import com.task.core.domain.DataPromptInfo;
import com.task.core.dto.request.DataPromptRequestDTO;
import com.task.core.mapper.DataPromptMapper;
import com.task.core.service.DataPromptService;
import com.task.core.vo.resp.DataPromptDetailRespVo;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;


@Service
public class DataPromptServiceImpl implements DataPromptService {

    @Resource
    private DataPromptMapper dataPromptMapper;


    @Override
    public void addPrompt(DataPromptRequestDTO request) {
        //1.参数校验
        verificationParam(request);

        //2.插入数据
        request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
        request.setCode(UUID.randomUUID().toString().replace("-", ""));
        dataPromptMapper.addPrompt(request);
    }

    /**
     * 获取脚本类型选项
     *
     * @param request
     * @return
     */
    @Override
    @DataScope(deptAlias = "dept", userAlias = "su")
    public List<KeyAndValue> getPromptForOption(DataPromptRequestDTO request) {
        return dataPromptMapper.getPromptForOption(request);
    }

    @Override
    @DataScope(deptAlias = "dept", userAlias = "su")
    public List<DataPromptInfo> getPromptByPage(DataPromptRequestDTO request) {
        return dataPromptMapper.getPromptByPage(request);
    }

    @Override
    public void modifyPrompt(DataPromptRequestDTO request) {
        if (StringUtils.isEmpty(request.getCode())) {
            throw new ServiceException(ErrorCodeConstants.PROPMT_CODE_IS_EMPTY.getMsg());
        }
        request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
        Integer integer = dataPromptMapper.modifyPrompt(request);
        if(integer == 0) {
            throw new ServiceException(ErrorCodeConstants.PROPMT_CODE_IS_EMPTY.getMsg());
        }
    }

    @Override
    public void deletePrompt(String PromptCode, Long userId) {
        Integer integer = dataPromptMapper.deletePrompt(PromptCode, userId);
        if(integer == 0) {
            throw new ServiceException(ErrorCodeConstants.PROPMT_CODE_IS_EMPTY.getMsg());
        }
    }

    @Override
    @DataScope(deptAlias = "dept",userAlias = "su")
    public DataPromptDetailRespVo getPromptDetail(String promptCode) {
        DataPromptRequestDTO dataPromptRequestDTO = new DataPromptRequestDTO();
        dataPromptRequestDTO.setCode(promptCode);
        return dataPromptMapper.getPromptDetail(dataPromptRequestDTO);
    }


    private void verificationParam(DataPromptRequestDTO request) {
        if(ObjectUtils.isEmpty(request.getContent())){
            throw new ServiceException(ErrorCodeConstants.PROMPT_IS_NULL.getMsg());
        }
    }
}
